package com.glorysoft.hlgdpda.ui.materialandtool.viewmodel

import com.glorysoft.hlgdpda.base.BaseViewModel
import com.glorysoft.hlgdpda.entity.ui.CallObserveBean
import com.glorysoft.hlgdpda.entity.ui.ToastMessageBean
import com.glorysoft.hlgdpda.model.TaskRepository
import com.google.gson.JsonObject

/**
 * FileName: BatchUnBindCarrierViewModel
 * Author: XJH
 * Date: 2021/12/14 16:08
 * Description:
 * History:
 */
class BatchUnBindCarrierViewModel : BaseViewModel() {

    val lotList: List<String> = listOf("批次号", "旧载具ID", "新载具ID")
    private val carrierRepository = TaskRepository()
    var unBindLotList = arrayListOf<String>()
    var newCarrierList = arrayListOf<String>()


    /**
     * 获取批次信息
     */
    fun searchLotInfo(lot: String) {
        if (unBindLotList.size >= 8){
            defUI.toastEvent.value = ToastMessageBean("一次最多更换8个载具",false)
            return
        }
        val map = JsonObject()
        map.addProperty("ACTIONTYPE", "LOTINFOBYID")
        map.addProperty("LOTID", lot)
        launchOnlyResult({
            val lotInfo = carrierRepository.getLotInfo(map)
            if (lotInfo.LOT.DURABLEID.isEmpty()){
                defUI.toastEvent.value = ToastMessageBean("当前批次没有绑定载具",false)
                return@launchOnlyResult
            }
            val map1 = JsonObject()
            map1.addProperty("LOTID", lotInfo.LOT.LOTID)
            map1.addProperty("CARRIERID", lotInfo.LOT.DURABLEID)
            carrierRepository.carrierUnBindLot(map1)
            unBindLotList.add(lotInfo.LOT.LOTID)
            defUI.callObserve.value = CallObserveBean(200, lotInfo)
        })
    }

    fun searchCarrierInfo(carrier: String) {
        if (unBindLotList.size >= 8){
            defUI.toastEvent.value = ToastMessageBean("一次最多更换8个载具",false)
            return
        }
        val map = JsonObject()
        map.addProperty("CARRIERID", carrier)
        launchOnlyResult({
            val lotInfo = carrierRepository.getLotInfoByCarrier(map)
            if (lotInfo.LOT.LOTID.isEmpty()){
                defUI.toastEvent.value = ToastMessageBean("当前载具没有绑定批次",false)
                return@launchOnlyResult
            }
            val map1 = JsonObject()
            map1.addProperty("LOTID", lotInfo.LOT.LOTID)
            map1.addProperty("CARRIERID", carrier)
            carrierRepository.carrierUnBindLot(map1)
            unBindLotList.add(lotInfo.LOT.LOTID)
            defUI.callObserve.value = CallObserveBean(200, lotInfo)
        })
    }

    fun replace(){
        val sb = StringBuilder()
        var lotID = ""
        var i = 0
        launchWithErrorAndComplete({
            unBindLotList.forEachIndexed { index, s ->
                lotID = s
                i = index
                val map = JsonObject()
                map.addProperty("LOTID", s)
                map.addProperty("CARRIERID", newCarrierList[index])
                carrierRepository.carrierBindLot(map)
            }
        }, { sb.append("$lotID;") }, {
            if (i == unBindLotList.size - 1) {
                defUI.callObserve.value = CallObserveBean(100, sb.toString())
            }
        })
    }

}